/* (PARI-GP) R. J. Cano, Thu April 17th 2014
 * 
 * Title: Connection with A137932
 * 
 * ("Recycling and mixing up some ideas taken from A215940");
 * 
 *  Sourcecode and sample output included.
 * 
 */

/* Definitions */

/*
 * The core of the present observations. Any of the 4 possible n*n triangular matrices that can be built.
 * (The "diagonalOnly" feature mentioned below actually is an exageration reducing them to diagonals)
 * 
 * Note for programmers:
 * Here was emphasized to have a simple and clear caller interface
 * instead of the best possible performance/efficency.
 * 
 */

triangular(size=2,kind=0,strict=1,upper=0,diagonalOnly=0)=if(!kind,matrix(size,size,i,j,(!diagonalOnly)*(upper*(i<j)+(!upper)*(i>j))+(!strict)*(i==j)),matrix(size,size,i,j,(!diagonalOnly)*(upper*(i+j<size+1)+(!upper)*(i+j>size+1))+(!strict)*(i+j==size+1)));

/* Note: By "kind" we mean: Which diagonal we are working with,
 * either "the diagonal" (main diagonal), or "the inverse diagonal"
 * (sometimes also called reverse diagonal) from an square matrix.
 */

palindroMatrix(n)=triangular(n,1,0,0,0)+triangular(n,0,0,0,0)+triangular(n,1,0,1,0)+triangular(n,0,0,1,0);

counter(n,k=2)={my(q=0,t=palindroMatrix(n));for(i=1,n,for(j=1,n,if(t[i,j]==k,q++)));q}

/* An observation */

tau(n)=(palindroMatrix(n)*vectorv(n,j,1))[n] /* All the components are identical in these results */

/*
 * tau() belongs to an arithmetic progression starting in 4 and running among its terms two by two,
 * this is: tau(j+1)=tau(j)+2;
 * 
*/

/* Specifications and analysis */

terms=25;
whichOne=1;

/* Ierations ( Note: whichOne==1, is excluded below since all these stuff there is trivially zero) */

print1("\n\nSome square matrices built by addition of triangular matrices:\n\n")

for(u=1,7,print("\n"palindroMatrix(u)));

/*
 * Notice this kind of matrices look symmetric regardless the way in which the are read.
 * 
 * (the whole matrix, its transpose, read by rows, read by cols, or by diagonals, anyway you look at them.
 * Nice!)
 * 
 * Note!, indeed, they're identical to their respective transposes.
 */

print1("\n\nCounting how many times the numbers 2, 3, and 4 appear in such matrices:\n\n");

/*
 * Please notice here that the first differences for the counters of 2, are identical to the
 * counters for 3.
 * 
 * If all the "strict" flags were enabled there in the definition for palindroMatrix(), then
 * the number 3 won't appear but this behavior wouldn't affect the counters for 2 alone.
 * 
 */

whichOne++;
w0=vector(terms,j,counter(j,whichOne));
w1=vector(#w0-1,j,w0[j+1]-w0[j]);
w2=vector(#w1-1,j,w1[j+1]-w1[j]);
print("\n\tCounters("whichOne"):\n"w0"\n\t1st Diffs:\n"w1"\n\t2nd Diffs:\n"w2)

whichOne++;
w0=vector(terms,j,counter(j,whichOne));
w1=vector(#w0-1,j,w0[j+1]-w0[j]);
w2=vector(#w1-1,j,w1[j+1]-w1[j]);
print("\n\tCounters("whichOne"):\n"w0"\n\t1st Diffs:\n"w1"\n\t2nd Diffs:\n"w2)

whichOne++;
w0=vector(terms,j,counter(j,whichOne));
w1=vector(#w0-1,j,w0[j+1]-w0[j]);
w2=vector(#w1-1,j,w1[j+1]-w1[j]);
print("\n\tCounters("whichOne"):\n"w0"\n\t1st Diffs:\n"w1"\n\t2nd Diffs:\n"w2)

/* By combining these counts. A little bit interesting the similarity with the preceding counters for 2 */

whichOne=2;
w0=vector(terms,j,counter(j,whichOne)+counter(j,whichOne+1));
w1=vector(#w0-1,j,w0[j+1]-w0[j]);
w2=vector(#w1-1,j,w1[j+1]-w1[j]);
print("\n\tCounters ("whichOne" and "whichOne+1" together):\n"w0"\n\t1st Diffs:\n"w1"\n\t2nd Diffs:\n"w2)

/* FOR COURTESY, A SAMPLE OUTPUT (Taken from a Linux 32bit machine):

----------------------------------------------------------------------------------------------------

bash-4.2$ clear; gp ./testingSomething_ThuApr17th2014.gp.txt 
Reading GPRC: /etc/gprc ...Done.

                           GP/PARI CALCULATOR Version 2.7.0 (released)
                    i686 running linux (ix86/GMP-5.1.3 kernel) 32-bit version
                         compiled: Apr 11 2014, gcc version 4.8.2 (GCC) 
                                     threading engine: single
                          (readline v5.2 enabled, extended help enabled)

                              Copyright (C) 2000-2014 The PARI Group

PARI/GP is free software, covered by the GNU General Public License, and comes WITHOUT ANY 
WARRANTY WHATSOEVER.

Type ? for help, \q to quit.
Type ?12 for how to get moral (and possibly technical) support.

parisize = 943718400, primelimit = 30000000

Some square matrices built by addition of triangular matrices:

[4]

[3, 3;
 3, 3]

[3, 2, 3;
 2, 4, 2;
 3, 2, 3]

[3, 2, 2, 3;
 2, 3, 3, 2;
 2, 3, 3, 2;
 3, 2, 2, 3]

[3, 2, 2, 2, 3;
 2, 3, 2, 3, 2;
 2, 2, 4, 2, 2;
 2, 3, 2, 3, 2;
 3, 2, 2, 2, 3]

[3, 2, 2, 2, 2, 3;
 2, 3, 2, 2, 3, 2;
 2, 2, 3, 3, 2, 2;
 2, 2, 3, 3, 2, 2;
 2, 3, 2, 2, 3, 2;
 3, 2, 2, 2, 2, 3]

[3, 2, 2, 2, 2, 2, 3;
 2, 3, 2, 2, 2, 3, 2;
 2, 2, 3, 2, 3, 2, 2;
 2, 2, 2, 4, 2, 2, 2;
 2, 2, 3, 2, 3, 2, 2;
 2, 3, 2, 2, 2, 3, 2;
 3, 2, 2, 2, 2, 2, 3]

Counting how many times the numbers 2, 3, and 4 appears in such matrices:

        Counters(2):
[0, 0, 4, 8, 16, 24, 36, 48, 64, 80, 100, 120, 144, 168, 196, 224, 256, 288, 324, 360, 400, 440, 484, 528, 576]
        1st Diffs:
[0, 4, 4, 8, 8, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 36, 36, 40, 40, 44, 44, 48]
        2nd Diffs:
[4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4]

        Counters(3):
[0, 4, 4, 8, 8, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 36, 36, 40, 40, 44, 44, 48, 48]
        1st Diffs:
[4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0]
        2nd Diffs:
[-4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4, 4, -4]

        Counters(4):
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
        1st Diffs:
[-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]
        2nd Diffs:
[2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 2]

        Counters(2 and 3 together):
[0, 4, 8, 16, 24, 36, 48, 64, 80, 100, 120, 144, 168, 196, 224, 256, 288, 324, 360, 400, 440, 484, 528, 576, 624]
        1st Diffs:
[4, 4, 8, 8, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 36, 36, 40, 40, 44, 44, 48, 48]
        2nd Diffs:
[0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0]

? \q
Goodbye!

 */